#!/bin/sh

# Slackware build script for mlocate

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Notes:
# - This is intended to be 100% compatible with the mlocate in -current.
# - mlocate and slocate can't read each others' database formats. Bummer.
# - DOWNLOAD is upstream's site, but the file is identical to the one
#   from Pat's source/ dir. I used upstream so the file won't disappear if
#   Pat upgrades or changes his mind.
# - mlocate on -current uses the slocate group (there's no mlocate group),
#   so I do the same here.
# - mlocate's database is about 40% larger than slocate's, on my build box.
#   Not a real concern unless you're building a tiny embedded system.

PRGNAM=mlocate
VERSION=${VERSION:-0.26}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var/lib \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make groupname=slocate
make install-strip DESTDIR=$PKG

gzip $PKG/usr/man/man?/*.?

# This stuff comes straight from Pat's mlocate.SlackBuild:
mkdir -p $PKG/etc
cp -a $CWD/updatedb.conf.new $PKG/etc/updatedb.conf.new
chown root:root $PKG/etc/updatedb.conf.new
chmod 644 $PKG/etc/updatedb.conf.new

mv $PKG/usr/bin/locate $PKG/usr/bin/mlocate
( cd $PKG/usr/bin ; ln -sf mlocate locate )

( cd $PKG/usr/man/man1
  ln -sf locate.1 mlocate.1
)

mkdir -p $PKG/usr/libexec
cp -a $CWD/mlocate-run-updatedb $PKG/usr/libexec/mlocate-run-updatedb
chown root:root $PKG/usr/libexec/mlocate-run-updatedb
chmod 755 $PKG/usr/libexec/mlocate-run-updatedb

mkdir -p $PKG/etc/cron.daily
cp -a $CWD/mlocate.cron $PKG/etc/cron.daily/mlocate
chown root:root $PKG/etc/cron.daily/mlocate
chmod 755 $PKG/etc/cron.daily/mlocate

chown root:slocate $PKG/usr/bin/mlocate
chmod 2711 $PKG/usr/bin/mlocate
mkdir -p $PKG/var/lib/mlocate
chown root:slocate $PKG/var/lib/mlocate
chmod 750 $PKG/var/lib/mlocate
# End of Pat's stuff.

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING* ChangeLog NEWS README* \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
